package org.kabeja;

import org.kabeja.dxf.DXFConstants;
import org.kabeja.dxf.DXFDocument;
import org.kabeja.dxf.entity.*;
import org.kabeja.parser.ParseException;
import org.kabeja.parser.ParserBuilder;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shutao
 * @since 2022/4/24 17:04
 */
public class MainTest {

    public static void main(String[] args) throws FileNotFoundException, ParseException{
        String filePath = "E:\\resource\\资源文件\\不动产测绘系统资源文件\\房屋测绘成果及规划图文件\\海伦堡·天悦府2-7、11-12栋预测绘数据\\单体图dxf\\FL8033W0_A10-3号楼平面图（出图）.dxf";
        long t = System.currentTimeMillis();
        DXFDocument doc = ParserBuilder.getDocument(filePath);
        System.out.println("解析耗时：" + (System.currentTimeMillis() - t) + "ms");

        List<DXFEntity> entities = doc.getEntities();
        List<DXFHatch> hatchEntities = getHatch(entities);

        System.out.println("end!");
    }

    /**
     * 获取全部填充图形
     * @author shutao
     * @date 2022/4/27 16:25
     * @param entities 图元列表
     */
    private static List<DXFHatch> getHatch(List<DXFEntity> entities) {
        List<DXFHatch> hatchEntities = new ArrayList<>();

        entities.forEach(entity -> {
            switch (entity.getType()) {
                case DXFConstants.ENTITY_TYPE_HATCH:
                    DXFHatch dxfHatch = (DXFHatch) entity;
                    dxfHatch.setHatchPattern(dxfHatch.getDXFDocument().getDXFHatchPattern(dxfHatch.getDXFHatchPatternID()));
                    hatchEntities.add(dxfHatch);
                    break;
                case DXFConstants.ENTITY_TYPE_INSERT:
                    DXFInsert dxfInsert = (DXFInsert) entity;
                    List<DXFHatch> hatchList = getHatch(entity.getDXFDocument().getDXFBlock(dxfInsert.getBlockID()).getEntities());
                    if (hatchList.size() > 0) {
                        hatchEntities.addAll(hatchList);
                    }
                    break;
                default:
                    break;
            }
        });
        return hatchEntities;
    }
}
